package cn.com.netedge.iot.frontend.datasave.utils;

import com.alibaba.fastjson2.JSON;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import cn.com.netedge.iot.common.constants.MqConstants;
import cn.com.netedge.iot.common.utils.DateUtil;
import cn.com.netedge.iot.frontend.datasave.model.freeze.ColumnDataModel;
import cn.com.netedge.iot.frontend.datasave.model.freeze.EventModel;
import cn.com.netedge.iot.frontend.datasave.model.freeze.FreezeDataModel;
import cn.com.netedge.iot.frontend.datasave.model.freeze.FreezeEventDataModel;
import cn.com.netedge.iot.model.event.EventModelWithJson;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class RabbitMQEventSender {
    private final static String QUEUE_NAME = MqConstants.QUEUE_DATA_C_EVENT_MESSAGE;

    public static void main(String[] args) {
        // 创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("172.18.100.14"); // RabbitMQ 服务所在地址
        factory.setPort(5672); // RabbitMQ 服务端口
        factory.setUsername("admin"); // 用户名
        factory.setPassword("admin"); // 密码
        //4:曲线2
//        testFreezeDataProducer(factory,MqConstants.QUEUE_DATA_C_LOAD2_MESSAGE,"LOAD2","data_c_lp2");
        //3:曲线1
//        testFreezeDataProducer(factory,MqConstants.QUEUE_DATA_C_LOAD_MESSAGE,"LOAD","data_c_lp1");
        //2：日冻结
        //testFreezeDataProducer(factory,MqConstants.QUEUE_DATA_C_DAILY_MESSAGE,"DAILY","data_c_daily");
        //1：月冻结
        //testFreezeDataProducer(factory,MqConstants.QUEUE_DATA_C_MONTHLY_MESSAGE,"MONTHLY","data_c_monthly");
//        //7:电表的VoltageQuality
//        testEventProducer(factory,MqConstants.QUEUE_DATA_C_EVENT_VOLTAGEQUALITY_MESSAGE,"FreezeEventDataModelVoltageQuality","data_e_voltagequality");
//        //6:电表的standard
        //testEventProducer(factory,MqConstants.QUEUE_DATA_C_EVENT_STANDARD_MESSAGE,"FreezeEventDataModelStandard","data_e_standard");
//        //5:电表的powerquality
//        testEventProducer(factory,MqConstants.QUEUE_DATA_C_EVENT_POWERQUALITY_MESSAGE,"FreezeEventDataModelPowerquality","data_e_powerquality");
//        //4:电表的powerfailure
        //testEventProducer(factory,MqConstants.QUEUE_DATA_C_EVENT_POWERFAILURE_MESSAGE,"FreezeEventDataModelPowerfailure","data_e_powerfailure");
//        //3：电表的frauddection事件
//        testEventProducer(factory,MqConstants.QUEUE_DATA_C_EVENT_FRAUDDETECTION_MESSAGE,"FreezeEventDataModelFrauddetection","data_e_frauddetection");
//       //2:电表的disconnector事件
        //testEventProducer(factory,MqConstants.QUEUE_DATA_C_EVENT_DISCONNECTOR_MESSAGE,"FreezeEventDataModelDisconnector","data_e_disconnector");
//        //1:电表的communication事件
        //testEventProducer(factory,MqConstants.QUEUE_DATA_C_EVENT_COMMUNICATION_MESSAGE,"FreezeEventDataModelCommunication","data_e_communication");
//       //0:测试集中器事件
//        testDCUEventProducer(factory);
    }
    //生成曲线、日、月冻结数据写对应队列中
    public static void testFreezeDataProducer(ConnectionFactory factory, String queueName, String dataType, String tableName)
    {
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            // 声明队列
            channel.queueDeclare(queueName, true, false, false, null);
            for (int i = 0; i < 5; i++) {


                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.add(calendar.DAY_OF_MONTH, -1);

                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                FreezeDataModel freezeDataModel = new FreezeDataModel();
                freezeDataModel.setMeterId(11);
                freezeDataModel.setTableName(tableName);

                if((dataType=="MONTHLY")||(dataType=="DAILY")||(dataType=="LOAD2"))
                {
                    freezeDataModel.getColumnDataModels().add(new ColumnDataModel("data_time",DateUtil.DateToLongStr(calendar.getTime()),"datetime"));
                    freezeDataModel.getColumnDataModels().add(new ColumnDataModel("KWH_IMPORT_ABS","0","numeric"));
                    freezeDataModel.getColumnDataModels().add(new ColumnDataModel("KWH_IMPORT_ABS_T1","0","numeric"));
                    freezeDataModel.getColumnDataModels().add(new ColumnDataModel("KWH_IMPORT_ABS_T2","0","numeric"));
                    freezeDataModel.getColumnDataModels().add(new ColumnDataModel("KWH_IMPORT_ABS_T3","0","numeric"));
                    freezeDataModel.getColumnDataModels().add(new ColumnDataModel("KWH_IMPORT_ABS_T4","0","numeric"));

                }
                if((dataType=="LOAD"))
                {
                    freezeDataModel.getColumnDataModels().add(new ColumnDataModel("data_time",DateUtil.DateToLongStr(calendar.getTime()),"datetime"));
                    freezeDataModel.getColumnDataModels().add(new ColumnDataModel("IA","5.001","numeric"));
                    freezeDataModel.getColumnDataModels().add(new ColumnDataModel("UA","220.01","numeric"));
                    freezeDataModel.getColumnDataModels().add(new ColumnDataModel("IB","5.002","numeric"));
                    freezeDataModel.getColumnDataModels().add(new ColumnDataModel("UB","220.02","numeric"));
                    freezeDataModel.getColumnDataModels().add(new ColumnDataModel("IC","5.003","numeric"));
                    freezeDataModel.getColumnDataModels().add(new ColumnDataModel("UC","220.03","numeric"));
                }
                //EventModelWithJson json = new EventModelWithJson();
                //json.setJsonData(JSON.toJSONString(freezeDataModel));
                //json.setDataType(dataType);
                String message = JSON.toJSONString(freezeDataModel);

                //System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date())+": [x] Sent 开始");
                // 发送消息
                channel.basicPublish("", queueName, null, message.getBytes());
                System.out.println("[x] Sent '" + message + "'");
                Thread.sleep(1000);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //生成事件记录写到对应队列中
    public static void testEventProducer(ConnectionFactory factory, String queueName, String dataType, String tableName)
    {
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            // 声明队列
            channel.queueDeclare(queueName, true, false, false, null);
            for (int i = 0; i < 5; i++) {


                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.add(calendar.DAY_OF_MONTH, -1);

                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                FreezeEventDataModel freezeEventDataModel = new FreezeEventDataModel();
                freezeEventDataModel.setProtocol(2);
                freezeEventDataModel.setDeviceType(0);
                freezeEventDataModel.setTableName(tableName);
                freezeEventDataModel.setDeviceId(11);
                if(dataType=="FreezeEventDataModelCommunication")
                {
                    if((i%2)==0)
                    {
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("event_id","215","numeric"));
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("data_time",DateUtil.DateToLongStr(calendar.getTime()),"datetime"));
                    }
                    else {
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("event_id","214","numeric"));
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("data_time",DateUtil.DateToLongStr(calendar.getTime()),"datetime"));
                    }
                }
                if(dataType=="FreezeEventDataModelDisconnector")
                {
                    if((i%2)==0)
                    {
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("event_id","62","numeric"));
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("data_time",DateUtil.DateToLongStr(calendar.getTime()),"datetime"));
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("KWH_IMPORT","0","numeric"));
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("KWH_EXPORT","0","numeric"));
                        //freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("KVARH_IMPORT","NULL","numeric"));
                        //freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("KVARH_EXPORT","NULL","numeric"));
                    }
                    else {
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("event_id","63","numeric"));
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("data_time",DateUtil.DateToLongStr(calendar.getTime()),"datetime"));
                        //freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("KWH_IMPORT","NULL","numeric"));
                        //freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("KWH_EXPORT","NULL","numeric"));
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("KVARH_IMPORT","0","numeric"));
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("KVARH_EXPORT","0","numeric"));
                    }
                }
                if(dataType=="FreezeEventDataModelFrauddetection")
                {
                    if((i%2)==0)
                    {
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("event_id","40","numeric"));
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("data_time",DateUtil.DateToLongStr(calendar.getTime()),"datetime"));
                    }
                    else {
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("event_id","41","numeric"));
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("data_time",DateUtil.DateToLongStr(calendar.getTime()),"datetime"));
                    }
                }
                if(dataType=="FreezeEventDataModelPowerfailure")
                {
                    if((i%2)==0)
                    {
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("event_id","1","numeric"));
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("data_time",DateUtil.DateToLongStr(calendar.getTime()),"datetime"));
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("KWH_IMPORT","0","numeric"));
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("KWH_EXPORT","0","numeric"));
                        //freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("KVARH_IMPORT","NULL","numeric"));
                        //freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("KVARH_EXPORT","NULL","numeric"));
                    }
                    else {
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("event_id","2","numeric"));
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("data_time",DateUtil.DateToLongStr(calendar.getTime()),"datetime"));
                        //freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("KWH_IMPORT","NULL","numeric"));
                        //freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("KWH_EXPORT","NULL","numeric"));
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("KVARH_IMPORT","0","numeric"));
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("KVARH_EXPORT","0","numeric"));
                    }
                }
                if(dataType=="FreezeEventDataModelPowerquality")
                {
                    if((i%2)==0)
                    {
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("event_id","95","numeric"));
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("data_time",DateUtil.DateToLongStr(calendar.getTime()),"datetime"));
                    }
                    else {
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("event_id","204","numeric"));
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("data_time",DateUtil.DateToLongStr(calendar.getTime()),"datetime"));
                    }
                }
                if(dataType=="FreezeEventDataModelStandard")
                {
                    if((i%2)==0)
                    {
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("event_id","5","numeric"));
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("data_time",DateUtil.DateToLongStr(calendar.getTime()),"datetime"));
                    }
                    else {
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("event_id","4","numeric"));
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("data_time",DateUtil.DateToLongStr(calendar.getTime()),"datetime"));
                    }
                }
                if(dataType=="FreezeEventDataModelVoltageQuality")
                {
                    if((i%2)==0)
                    {
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("event_id","5","numeric"));
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("data_time",DateUtil.DateToLongStr(calendar.getTime()),"datetime"));
                    }
                    else {
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("event_id","4","numeric"));
                        freezeEventDataModel.getColumnDataModels().add(new ColumnDataModel("data_time",DateUtil.DateToLongStr(calendar.getTime()),"datetime"));
                    }
                }

                /*EventModelWithJson json = new EventModelWithJson();
                json.setJsonData(JSON.toJSONString(freezeEventDataModel));
                json.setDataType(dataType);
                String message = JSON.toJSONString(json);*/
                String message = JSON.toJSONString(freezeEventDataModel);

                //System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date())+": [x] Sent 开始");
                // 发送消息
                channel.basicPublish("", queueName, null, message.getBytes());
                System.out.println("[x] Sent '" + message + "'");
                Thread.sleep(1000);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //0: DCU Event
    public static void testDCUEventProducer(ConnectionFactory factory)
    {
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            // 声明队列
            channel.queueDeclare(QUEUE_NAME, true, false, false, null);
            for (int i = 0; i < 2; i++) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.add(calendar.DAY_OF_MONTH, -1);

                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                EventModel eventModel = new EventModel();
                eventModel.setEvtTime(formatter.parse(DateUtil.DateToLongStr(calendar.getTime())));
                eventModel.setEvtId(6);
                eventModel.setDeviceId(11);
                eventModel.setProtocol(2);
                eventModel.setDeviceType(1);
                eventModel.setSaveDBTime(formatter.parse(DateUtil.DateToLongStr(new Date())));
                eventModel.setDescription("测试事件入库");
                EventModelWithJson json = new EventModelWithJson();
                json.setJsonData(JSON.toJSONString(eventModel));
                json.setDataType("EventModel");
                String message = JSON.toJSONString(json);

                // 发送消息
                channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
                System.out.println(" [x] Sent '" + message + "'");
                Thread.sleep(5000);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

